package daosimpl;

import java.util.ArrayList;
import java.util.List;

import model.Alumno;
import model.CursoPeridoGrupo;
import model.Examen;
import model.Pregunta;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import util.HibernateUtil;
import daos.ExamenDAO;
import daos.PreguntaDAO;



public class ExamenDAOImpIH implements ExamenDAO{
	private Transaction tx;
	public Examen insertexamen(Examen p)  throws HibernateException{
	try {
			
			tx = HibernateUtil.session.beginTransaction();
			HibernateUtil.session.save(p);
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}
			return p;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return null;        	
	}
	public List<Examen> ListarExamenes() throws HibernateException{
		
		List<Examen> result = new ArrayList<Examen>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Examen");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
		
	}
	@Override
	public List<Examen> ListarExamenes(CursoPeridoGrupo cpg)
			throws HibernateException {
		List<Examen> result = new ArrayList<Examen>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Examen as exam");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}
	
	@Override
	public List<Examen> ListarExamenesxAlumno(Alumno a)
			throws HibernateException {
		List<Examen> result = new ArrayList<Examen>();
		try {			
			tx = HibernateUtil.session.beginTransaction();						
			Query query = HibernateUtil.session
					.createQuery("FROM Examen where Alumno_alumno_id='"+a.getAlumnoId()+"'");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}
}
